<?php

require_once 'MainPageWidget.php';
class RssWidget extends MainPageWidget
{
	public $title = 'RSS';
	public $items = array(); // список RSS лент

	public function initWidget($parent)
	{
		$router = $this->ctx->rh->router;
		$params = $this->parent->params;

		$this->ctx->rh->UseClass('RL_URLGenerator');

		// построим ссылки на RSS ленты
		if (isset($params['community_url'])) {
			// RSS community feed
			$rule = "rss_community_feed";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Все записи сообщества';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem('rss_community_feed', $mi);

			// RSS community comments
			$rule = "rss_community_comments";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Все комментарии сообщества';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem($rule, $mi);
		}

		if (isset($params['tdi_url'])) {
			// RSS community feed in the tdi
			$rule = "rss_community_tdi_feed";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Записи раздела';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem($rule, $mi);

			// RSS community comments in the tdi
			$rule = "rss_community_tdi_comments";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Комментарии раздела';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem($rule, $mi);
		}

		if (isset($params['user_url'])) {
			// RSS user feed
			$rule = "rss_user_feed";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Записи пользователя';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem($rule, $mi);

			// RSS user comments
			$rule = "rss_user_comments";
			$ug = new RL_URLGenerator($router->getRule($rule));
			$ug->setRouter($router);
			$ug->setParams($params);
			$mi = array();
			$mi['title'] = 'Комментарии пользователя';
			$mi['url'] = $ug->generateUrl();
			$this->addMenuItem($rule, $mi);
		}

		return $this;
	}

	public function addMenuItem ($name, $mi)
	{
		$this->items[$name] = $mi;
	} // end of method addMenuItem
	public function buildMenuItemFromTdi ($tdi)
	{
		$mi = $tdi->data;
		$mi['url']  = $this->tdiUrl($tdi);
		return $mi;
	} // end of method buildMenuItemFromTdi
	public function tdiUrl ($tdi)
	{
		$this->ctx->rh->UseClass('RL_URLGenerator');
		$router = $this->ctx->rh->router;
		$params = $this->parent->params;
		$params['tdi_url'] = $tdi->data['url'];
		$ug = new RL_URLGenerator($router->getRule('community_tdi'));
		$ug->setRouter($router);
		$ug->setParams($params);
		return $ug->generateUrl();
	} // end of method getTdiUrl

}

